package com.chenyun.boot.datasource.page;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.chenyun.common.constants.SysConstants;
import com.chenyun.common.context.ServletContext;
import com.chenyun.common.vo.QueryPage;
import com.chenyun.common.vo.SimpleQueryPage;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

/***
 * 分页的切面
 * 规定  分页参数必须是pageIndex 和 pageSize
 */
@Component
@Aspect
public class PageAspect {

    public HttpServletRequest servletRequest=null;
    public PageAspect(HttpServletRequest servletRequest){
        this.servletRequest=servletRequest;
    }

    protected final Logger logger = LoggerFactory.getLogger(PageAspect.class);

    @Pointcut("@annotation(com.chenyun.common.annotation.Page) ")
    public void pointcat() {
    }

    @Before(value = "pointcat()")
    public void doBefore(JoinPoint point) {
        logger.info("进入分页切面,解析参数分页......");
        Object[] args = point.getArgs();
        //拿到方法上的参数
        Map<String, String[]> parameterMap1 = servletRequest.getParameterMap();
        String queryString = servletRequest.getQueryString();
        Enumeration<String> parameterNames1 = servletRequest.getParameterNames();


        String[] parameterNames = ((MethodSignature) point.getSignature()).getParameterNames();
        int pageInex = 0;
        int pageSize = 0;
        String orderBy = "";
        //规定  分页参数必须是pageNum 和 pageSize
        int pageIndexindex = ArrayUtils.indexOf(parameterNames, SysConstants.PAGE_NUM);
        if (pageIndexindex==-1){
            pageIndexindex = ArrayUtils.indexOf(parameterNames, SysConstants.PAGE_INDEX);
        }
        int pageSizeindex = ArrayUtils.indexOf(parameterNames, SysConstants.PAGE_SIZE);


        //必须是QueryPage 或param方式
        if (point.getArgs()[0] instanceof QueryPage) {
            QueryPage arg = (QueryPage) point.getArgs()[0];
            pageInex = arg.getPageIndex();
            pageSize = arg.getPageSize();
            orderBy = arg.getOrderBy();
        }
         else  if (point.getArgs()[0] instanceof SimpleQueryPage){
             //使用简单分页对象
            SimpleQueryPage arg = (SimpleQueryPage) point.getArgs()[0];
            pageInex = arg.getPageIndex();
            pageSize = arg.getPageSize();
        }
        else if (pageIndexindex!=-1&&pageSizeindex!=-1){
            if (pageIndexindex != -1 && pageSizeindex != -1) {
                pageInex = Integer.valueOf(args[pageIndexindex].toString());
                pageSize = Integer.valueOf(args[pageSizeindex].toString());
            }
        }else {
            //拿到query中的
            final Map<String, String[]> parameterMap = ServletContext.getParameterMap();
            if (parameterMap.containsKey(SysConstants.PAGE_SIZE)) {
                String[] strings = parameterMap.get(SysConstants.PAGE_NUM);
                if (strings==null||strings.length==0){
                 strings=parameterMap.get(SysConstants.PAGE_INDEX);
                }
                if (NumberUtil.isNumber(strings[0])){
                    pageInex=Integer.parseInt(strings[0]);
                    pageSize=Integer.parseInt(parameterMap.get(SysConstants.PAGE_SIZE)[0]);
                }
            }
        }
        if (pageSize > 0) {
            logger.info("解析分页完成,当前请求开启分页查询.....pageInex:{},pageSize:{}",pageInex,pageSize);
            //如果条数大于0才去分页
            // reasonable 分页参数合理化:值当传入的页数大于数据总页数时,会将页数设置为当前最大的页数
            if (StringUtils.isNotBlank(orderBy)){
                PageHelper.orderBy(orderBy);
            }
            PageHelper.startPage(pageInex, pageSize).setReasonable(false);
        }
        return;

    }


}
